
//
// 120205 - AH - (Opera) Created.
// 120407a- AH - (Midtun) Missing java doc and change to toString().
// 120407b- AH - (Midtun) Name property.
//

package org.soekrat.engine.motor.passingstrategies;

import java.util.ArrayList;
import java.util.List;

import javax.sound.midi.Sequence;

import org.soekrat.engine.wheel.Wheel;
import org.soekrat.engine.wheel.ontology.ForestOntology;
import org.soekrat.engine.wheel.ontology.MineOntology;
import org.soekrat.engine.wheel.ontology.NetworkOntology;
import org.soekrat.engine.wheel.ontology.Ontology;
import org.soekrat.engine.wheel.ontology.ProductTableOntology;

/**
 * <p>
 *   Base class for 
 *   {@link PassingStrategy} objects that do maintane a sequence of ontology
 *   names. 
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public abstract class SequenceBasedPassingStrategy extends PassingStrategy 
{
  protected final List<String> m_seq=new ArrayList<String>();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param name Name of strategy.
   */
  protected SequenceBasedPassingStrategy(String name)
  {
    super(name);
    
    // Default sequence, suite stuff.
    m_seq.add(ProductTableOntology.class.getName());
    m_seq.add(NetworkOntology.class.getName());
    m_seq.add(ForestOntology.class.getName());
    m_seq.add(MineOntology.class.getName());
  }
  
  /**
   * <p>
   *   Sets
   *   {@link Sequence} from the 
   *   {@link Wheel}.
   * </p>
   * @param wheel {@link Wheel}.
   */
  public void setSequence(Wheel wheel)
  {
    int n=wheel.getNumberOfOntologies();
    for (int i=0; i<n; i++) 
    {
      String name=wheel.getOntology(i).getClass().getName();
      m_seq.add(name);
    }
  }
  
  /**
   * <p>
   *   Sets sequence of ontologies.
   * </p>
   * @param names Names on ontologies.
   */
  public void setSequence(List<String> names)
  {
    if (names==null)
    {
      throw new NullPointerException("l");
    }
    
    m_seq.clear();
    
    int n=names.size();
    for (int i=0; i<n; i++)
    {
      String name=names.get(i);
      if (name==null)
      {
        throw new NullPointerException("l["+i+"]");
      }
      
      m_seq.add(name);
    }
  }
  
  /**
   * <p>
   *   Gets the names of 
   *   {@link Ontology ontologies} in sequence. 
   * </p>
   * @return Names. 
   */
  public final String[] getSeguence(){ return m_seq.toArray(new String[0]); }
  
  @Override
  public String toString() 
  {
    StringBuilder sb=new StringBuilder();
    sb.append("sequence=");
    
    boolean first=true;
    for (String oname : m_seq)
    {
      if (!first) sb.append(",");
      first=false;
      sb.append(oname);
    }
    
    return sb.toString();
  }
  
}
